package com.hdcx.Minfresh.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class LVCircularRing extends View {

private float mWidth = 0f;
private float mPadding = 0f;
private float startAngle = 0f;
private Paint mPaint;

public LVCircularRing(Context context) {
    this(context, null);
}

public LVCircularRing(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public LVCircularRing(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaint();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    if (getMeasuredWidth() > getHeight())
        mWidth = getMeasuredHeight();
    else
        mWidth = getMeasuredWidth();
    mPadding = 5;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    mPaint.setColor(Color.argb(100, 255, 255, 255));
    canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaint);
    mPaint.setColor(Color.WHITE);
    RectF rectF = new RectF(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
    canvas.drawArc(rectF, startAngle, 100
            , false, mPaint);//第四个参数是否显示半径

}


private void initPaint() {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setColor(Color.WHITE);
    mPaint.setStrokeWidth(8);
}

public void startAnim() {
    stopAnim();
    startViewAnim(0f, 1f, 1000);
}

public void stopAnim() {
    if (valueAnimator != null) {
        clearAnimation();
        valueAnimator.setRepeatCount(1);
        valueAnimator.cancel();
        valueAnimator.end();
    }
}

ValueAnimator valueAnimator;

private ValueAnimator startViewAnim(float startF, final float endF, long time) {
    valueAnimator = ValueAnimator.ofFloat(startF, endF);

    valueAnimator.setDuration(time);
    valueAnimator.setInterpolator(new LinearInterpolator());
    valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//无限循环
    valueAnimator.setRepeatMode(ValueAnimator.RESTART);//

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {

            float value = (float) valueAnimator.getAnimatedValue();
            startAngle = 360 * value;

            invalidate();
        }
    });
    valueAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
        }
    });
    if (!valueAnimator.isRunning()) {
        valueAnimator.start();
    }

    return valueAnimator;
}
}